<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Smiles/classes/PhpFox_ComponentList.class.php, $Author:  $ */



/** Display list bulletins
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.bulletin
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Smiles_ComponentList extends BaseComponent
{
	function process()
	{
        $oReq = &App::getRequest();
        $oSrvSmile = $this->_oModule->getService('Smiles');
        $sDir = opendir(App::getParam('smile.path').'emo/');
        $oTpl = &$this->_oModule->getTemplate();
    	//action block
        if($oReq->get('del'))
        {
            $oSrvSmile->delItems($oReq->get('del'));
            $aErrors = $oSrvSmile->getErrors();

            $aMess = (empty($aErrors)) ? array(App::format('Mod_Smiles.item.deleted')) : $aErrors;
            $bFlag = (empty($aErrors)) ? false : true;

            App::gotoUrl('admin.extra.smilies',array(), $aMess, $bFlag, 'Smile');
        }
        //edit block
        if($oReq->get('edit'))
        {
            $sFile = base64_decode($oReq->get('edit'));

            $aFields = array(
                    			'smcode'   => array('title'=>App::format('Mod_Smiles.smiley_code'), 'pattern'=>'/^[a-zA-Z\d\_]{1,30}$/'),
                    	   );

            $oValidator = &App::newObject('Validator', $aFields);

            if($oReq->getAction() == 'btnRen')
            {
                if($oValidator->isValid($oReq->getAll()))
                {
                    list($sName, $sExt) = explode('.', $sFile);
                    $oSrvSmile->renameSmile($sFile, $oReq->get('smcode'), $sExt);
                    $aErrors = $oSrvSmile->getErrors();

                    $aMess = (empty($aErrors)) ? array(App::format('Mod_Smiles.item.updated')) : $aErrors;
                    $bFlag = (empty($aErrors)) ? false : true;
                    App::gotoUrl('admin.extra.smilies',array(), $aMess, $bFlag, 'Smile');
                }
                else
                    $this->addErrors($oValidator->getErrors());
            }

            $sName = str_replace('.jpg', '', $sFile);
            $sName = str_replace('.gif', '', $sFile);
            $oTpl->assignSrc(array(
                                    'jsValidation' => $oValidator->makeJS(),
		                            'jsValMess'    => $oValidator->makeJsMess(),
		                            'aErrors'	   => $this->getErrors(),
		                            'sFile'        => $sName,
		                            'sFullLink'    => App::getParam('smile.url').'emo/'.$sFile,

            ));
            $srcHtml = $oTpl->fetch('Edit.html');
            return $srcHtml;
        }

        // upload block
        if($oReq->get('upload'))
        {
            if($oReq->getAction() == 'btnUpl')
            {
                if($oSrvSmile->uploadSmiles($_FILES))
                    App::gotoUrl('admin.extra.smilies', array(), array(App::format('Mod_Smiles.item.uploaded')), false, 'Smile');
                else
                    $this->addErrors($oSrvSmile->getErrors());
            }

            $oTpl->assignSrc(array(
		                            'aErrors'	   => $this->getErrors(),
		                            'aLimit'       => App::getParam('smilie'),
            ));
            $srcHtml = $oTpl->fetch('Upload.html');
            return $srcHtml;
        }

    	while ($sFile = readdir($sDir))
    	{
    		if (ereg("jpg",$sFile) || ereg("gif",$sFile))
    		{
    			$sName = str_replace(".jpg","",$sFile);
    			$sName = str_replace(".gif","",$sName);
    			$sFullName = App::getParam('smile.path').'emo/'.$sName.'.gif';
    			$aFile[$sName] = array('file'=>$sFile, 'is_wr'=>is_writeable($sFullName), 'code'=>base64_encode($sFile));
    		}
    	}
    	if(!empty($aFile))
    	    arsort($aFile, SORT_ASC);

    	$oTpl->assignSrc(array(
                                'aFile'      => !empty($aFile) ? $aFile : false,
                                'sSmUrl'    => App::getParam('smile.url').'emo/',
                                )
        );

        $srcHtml = $oTpl->fetch('Smiles.html');
		return $srcHtml;
	}
}
?>